<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UpDomEl
 *
 * @author david
 */
class UpDomEl
{
    private $domNode = null;
    private $dom = null;

    public function  __construct($node, $dom)
    {
        $this->domNode = $node;
        $this->dom = $dom;
    }

    // append a new element to the DOM structure
    public function appLast ($_el, $_ref )
    {
        //
        $el = htmlentities($_el);

        $newNode = $this->dom->createElement( $el );      

        // insert the id or class attribute
        $type = substr($_ref, 0,1);
        $value = substr ($_ref,1);

        switch ($type)
        {
            case '#':
                $newNode->setAttribute ('id', $value);
                break;

            case '.':
                $newNode->setAttribute ('class', $value);
                break;
        }
        
        $this->domNode->appendChild($newNode);

        return new UpDomEl($newNode, $this->dom);

    }


    public function appFirst ($_el , $_ref)
    {
        //
        $el = htmlentities($_el);

        $newNode = $this->dom->createElement( $el );

         // insert the id or class attribute
        $type = substr($_ref, 0,1);
        $value = substr ($_ref,1);

        switch ($type)
        {
            case '#':
                $newNode->setAttribute ('id', $value);
                break;

            case '.':
                $newNode->setAttribute ('class', $value);
                break;
        }

         $this->domNode->insertBefore($newNode, $this->domNode->firstChild);

        return new UpDomEl($newNode, $this->dom);
    }


    public function before ($_el , $_ref)
    {
        //
        $el = htmlentities($_el);

        $newNode = $this->dom->createElement( $el );

         // insert the id or class attribute
        $type = substr($_ref, 0,1);
        $value = substr ($_ref,1);

        switch ($type)
        {
            case '#':
                $newNode->setAttribute ('id', $value);
                break;

            case '.':
                $newNode->setAttribute ('class', $value);
                break;
        }

        $this->domNode->parentNode->insertBefore($newNode, $this->domNode);

        return new UpDomEl($newNode, $this->dom);

    }


    public function after ($_el , $_ref)
    {
        //
        $el = htmlentities($_el);

        $newNode = $this->dom->createElement( $el );

         // insert the id or class attribute
        $type = substr($_ref, 0,1);
        $value = substr ($_ref,1);

        switch ($type)
        {
            case '#':
                $newNode->setAttribute ('id', $value);
                break;

            case '.':
                $newNode->setAttribute ('class', $value);
                break;
        }

        $this->domNode->parentNode->insertBefore($newNode, $this->domNode->nextSibling);

        return new UpDomEl($newNode, $this->dom);

    }



    public function attr ($_name, $_value)
    {
        $name = htmlentities($_name);
        $value = htmlentities($_value);

        $this->domNode->setAttribute ($name, $value);

        return $this;
    }

    public function text ($_txt)
    {
        if ($this->domNode == null)
        {
            return null;
        }

        $txt = htmlentities($_txt);

        $newTextNode = $this->dom->createTextNode( $txt );

        $this->domNode->appendChild($newTextNode);

        return $this;
    }
	
    public function css ($_key, $_val)
    {
        if ($this->domNode == null)
        {
            return null;
        }

        $key = htmlentities($_key);
        $val = htmlentities($_val);

        if ($this->domNode->hasAttribute('style'))
        {
            $prevStyles = $this->domNode->getAttribute('style');

            $newStyle = $prevStyles.";".$key.': '.$val;

            $this->domNode->setAttribute ('style', $newStyle);
        }
        else
        {
            $this->domNode->setAttribute ('style', $key.': '.$val);
        }

        return $this;
    }


    public function subst ($_el)
    {
        $el = htmlentities($_el);

        $newNode = $this->dom->createElement( $el );

        $this->domNode->parentNode->insertBefore($newNode, $this->domNode);
        $this->domNode->parentNode->removeChild($this->domNode);

        return new UpDomEl($newNode, $this->dom);
    }


    public function clear ()
    {
        while (isset($this->domNode->firstChild))
        {            
            $this->domNode->removeChild($this->domNode->firstChild);
        }
    }

    public function getHtml ()
    {
        if ($this->domNode == null)
        {
            
        }
    }

    public function getAttr ()
    {
        
    }


}
?>
